//package com.ruoyi.business.utils;
//
//import com.google.zxing.BarcodeFormat;
//import com.google.zxing.EncodeHintType;
//import com.google.zxing.MultiFormatWriter;
//import com.google.zxing.WriterException;
//import com.google.zxing.client.j2se.MatrixToImageWriter;
//import com.google.zxing.common.BitMatrix;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.codec.binary.Base64;
//import org.apache.commons.io.FileUtils;
//
//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.IOException;
//import java.util.HashMap;
//import java.util.Map;
//
//@Slf4j
//public final class PDFUtils {
//
//    public static final String IMAGE_PNG_BASE_64 ="data:image/png;base64,";
//
//    /**
//     * 填充pdf模板
//     * @param templateUrl 填充的模板地址
//     * @param outputFileUrl 导出的模板地址
//     * @param fieldMap 填充的字段map
//     * @param imageMap 填充的图片
//     * @return 填充的结果
//     */
//    public static void fillPdfTemplate(String templateUrl, String outputFileUrl, Map<String, String> fieldMap, Map<String, String> imageMap) {
////        OutputStream os = null;
////        PdfStamper ps = null;
////        PdfReader reader = null;
////        AcroFields form = null;
////
////        try {
////            os = Files.newOutputStream(new File(outputFileUrl).toPath());
////            reader = new PdfReader(Files.newInputStream(Paths.get(templateUrl)));
////            ps = new PdfStamper(reader, os);
////            form = ps.getAcroFields();
////
////            // 添加中文字体支持（可选）
////            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
////            form.addSubstitutionFont(bf);
////
////            // 填充表单字段
////            for (Map.Entry<String, String> entry : fieldMap.entrySet()) {
////                if(entry.getValue()!=null){
////                    form.setFieldProperty(entry.getKey(),"textfont",bf,null);
////                    form.setField(entry.getKey(), entry.getValue());
////                }
////            }
////
////            // 插入图片
////            for (Map.Entry<String, String> entry : imageMap.entrySet()) {
////                if (entry.getValue()!=null){
////                    if(entry.getValue().startsWith(IMAGE_PNG_BASE_64)){
////                        insertImageByBase64(form, ps, entry.getKey(), entry.getValue());
////                    }else{
////                        insertImage(form, ps, entry.getKey(), entry.getValue());
////                    }
////                }
////            }
////            ps.setFormFlattening(true);
////        } catch (Exception e) {
////            log.error("pdf模板填充失败！！！",e);
////            throw new BaseException("pdf模板填充失败！！！");
////        } finally {
////            try {
////                if (ps != null) ps.close();
////                if (reader != null) reader.close();
////                if (os != null) os.close();
////            } catch (IOException | DocumentException e) {
////                log.error("文件流关闭失败！！！",e);
////            }
////        }
//    }
//
//
////    public static void insertImage(AcroFields form, PdfStamper ps, String fieldName,String imageUrl) throws IOException, DocumentException {
////        if(imageUrl!=null){
////            List<AcroFields.FieldPosition> fieldPositions = form.getFieldPositions(fieldName);
////            if(fieldPositions==null){
////                return;
////            }
////            for (int i = 0; i < fieldPositions.size(); i++) {
////                //获取fieldName所在的页码
////                int pageNo = form.getFieldPositions(fieldName).get(i).page;
////                Rectangle signRect = form.getFieldPositions(fieldName).get(i).position;
////                float x = signRect.getLeft();
////                float y = signRect.getBottom();
////                Image image = Image.getInstance(imageUrl);
////                // 获取操作的页面
////                PdfContentByte overContent = ps.getOverContent(pageNo);
////                // 根据域的大小缩放图片
////                image.scaleToFit(signRect.getWidth(), signRect.getHeight());
////                // 添加图片
////                image.setAbsolutePosition(x, y);
////                overContent.addImage(image);
//////                ps.setFormFlattening(false);
////            }
////        }
////
////    }
////
////    public static void insertImageByBase64(AcroFields form, PdfStamper ps, String fieldName,String imageBase64) throws IOException, DocumentException {
////        if(imageBase64!=null){
////            List<AcroFields.FieldPosition> fieldPositions = form.getFieldPositions(fieldName);
////            if(fieldPositions==null){
////                return;
////            }
////            for (int i = 0; i < fieldPositions.size(); i++) {
////                //获取fieldName所在的页码
////                int pageNo = form.getFieldPositions(fieldName).get(i).page;
////                Rectangle signRect = form.getFieldPositions(fieldName).get(i).position;
////                float x = signRect.getLeft();
////                float y = signRect.getBottom();
////                imageBase64 = imageBase64.replace(IMAGE_PNG_BASE_64,"");
////                byte[] bytes = Base64.decodeBase64(imageBase64);
////                Image image = Image.getInstance(bytes);
////                image.setTransparency(new int[]{Color.WHITE.getRGB()}); // 设置白色为透明背景
////                // 获取操作的页面
////                PdfContentByte overContent = ps.getOverContent(pageNo);
////                // 根据域的大小缩放图片
////                // 获取填充的宽度和高度
////                float pageWidth = signRect.getWidth();
////                float pageHeight = signRect.getHeight();
////                image.scaleToFit(pageWidth*2, pageHeight*2);
//////                // 添加图片
////                image.setAbsolutePosition((x + pageWidth/8), y-pageHeight/4);
////                overContent.addImage(image);
//////                ps.setFormFlattening(false);
////            }
////        }
////
////    }
//
//    public static String generateQRCodeImage(String text, int width, int height) throws WriterException, IOException {
//        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        Map<EncodeHintType, Object> hints = new HashMap<>();
//        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 设置编码格式为UTF-8
//        hints.put(EncodeHintType.MARGIN, 0);
//        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
//        MatrixToImageWriter.writeToStream(bitMatrix, "PNG", baos); // 输出图片格式为PNG
//        byte[] imageBytes = baos.toByteArray(); // 获取字节数据
//        String base64Image = Base64.encodeBase64String(imageBytes); // 将字节数据编码为Base64字符串
//        return  IMAGE_PNG_BASE_64 + base64Image; // 创建img标签的src属性值
//    }
//
//
//    public static void main(String[] args) throws IOException, WriterException {
//        String projectSignPath = System.getProperty("user.dir") + "/uploadPath/sign";
//        String signTemplate = projectSignPath+"/template/华山医院进修生告知书.pdf";
//        String signFile = "D:\\工作空间\\签名照片.jpg";
//        String signOutTemplate = projectSignPath+"/files/华山医院进修生告知书-测试.pdf";
////        String signOutTemplate1 = projectSignPath+"/files/华山医院进修生告知书-测试1.pdf";
//        String base64String = Base64.encodeBase64String(FileUtils.readFileToByteArray(new File(signFile)));
//        base64String = IMAGE_PNG_BASE_64 +base64String;
//        Map<String,String> txtMap = new HashMap<>();
//        txtMap.put("deptName","ETC");
//        txtMap.put("sureYear","2025");
//        txtMap.put("sureMonth","03");
//        txtMap.put("sureDate","26");
//        Map<String,String> imgMap = new HashMap<>();
//        imgMap.put("selfSign",base64String);
//        fillPdfTemplate(signTemplate,signOutTemplate,txtMap,imgMap);
//
////        System.out.println(generateQRCodeImage("测试",300,300));
//    }
//
//
//}
//
